#!/usr/bin/env python
# -*- coding: cp1250 -*-
#sys.path.append(r'C:\x\y')

"""
PErsonal Python Enhancements library (general purpose and data-mining)

Copyright (C) 2007-2009, Viktor Presovsky <vpresovsky@deloitteCE.com>
Copyright (C) 2003-2007, Viktor Presovsky <presovsky@gmail.com>
Copyright (C) 2009, Michal Sagat <msagat@deloitteCE.com>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""

import decimal, re, string, sys, os, time, glob, datetime

import pepe.p_filesystem.basic as pfb
import pepe.p_dm.sdb as she
#import pepe.p_win32.getOsVersion as pwg

## STRINGs #########################################

remove_html_tags = lambda x: re.sub('<([^!>]([^>]|\n)*)>', '', x)   # remove html tags 

zf = lambda x, y: string.zfill(str(x), y)
space_fill = lambda x, y: x + ((y - len(x)) * ' ')
"""
print space_fill('aa', 20), '-'
>>> aa                   -

print space_fill('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaasss', 20), '-'
>>> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaasss -
"""

srj = lambda x, y: string.rjust(str(x), y)
slj = lambda x, y: string.ljust(str(x), y)
l2s = lambda l, s: string.join(l, s)
ss = lambda x: string.strip(x)

sstrip = lambda x: string.strip(x)
stight = lambda x: re.sub('\s','',x)

jl = lambda x_l: string.join([str(x) for x in x_l],"|")

# Efficient String Concatenation in Python
# http://www.skymind.com/~ocrow/python_string/
esc = lambda x_l : ''.join([`num` for num in x_l])
#print esc([1,2,3])

## NUMERICs #########################################

D = decimal.Decimal

numczen = lambda x: re.sub(',', '.', re.sub('\.', '', stight(x)))
numencz = lambda x: re.sub('\.', ',', re.sub(',', '', stight(x)))

# 'dotcoma to dot' converts 4.063.439,32 ... 4063439.32 
dc2d = lambda x: re.sub(',', '.', re.sub('\.', '', stight(x)))
# 'comadot to coma' converts 4,063,439.32 ... 4063439,32 
cd2c = lambda x: re.sub('\.', ',', re.sub(',', '', stight(x)))

def minus(s):
    res = ''
    if string.find(s,'-') != -1:
        res = '-' + re.sub('-','',s)
    else:
        res = s
    return res

## LISTs #############################################

# compare, addup, deduct, multiply
LCompare = lambda a_l, b_l: [(x,y) for (x, y) in zip(a_l, b_l) if x != y]
LJoin = lambda x, y: [ str(a)+':'+str(b)  for (a, b) in zip(x, y)]
#print LJoin(['a','b'],['c','d'])
LAddup = lambda a_l, b_l: [ x+y  for (x, y) in zip(a_l, b_l)]
LDeduct = lambda a_l, b_l: [ x-y  for (x, y) in zip(a_l, b_l)]
LMultiply = lambda a_l, b_l: [ x*y  for (x, y) in zip(a_l, b_l)]

joinLists = lambda x, y: [ str(a)+str(b)  for (a, b) in zip(x, y)]
#>>> print joinLists(['a','b'],['c','d'])
#['ac', 'bd']

LCompare = lambda a_l, b_l: [(x,y) for (x, y) in zip(a_l, b_l) if x != y]
LAddup = lambda a_l, b_l: [ x+y  for (x, y) in zip(a_l, b_l)]
LDeduct = lambda a_l, b_l: [ x-y  for (x, y) in zip(a_l, b_l)]
LMultiply = lambda a_l, b_l: [ x*y  for (x, y) in zip(a_l, b_l)]
LJoin = lambda x, y: [ str(a)+':'+str(b)  for (a, b) in zip(x, y)]
joinLists = lambda x, y: [ str(a)+str(b)  for (a, b) in zip(x, y)]
l2s = lambda l, s: string.join(l, s)
numczen = lambda x: re.sub(',', '.', re.sub('\.', '', stight(x)))
numencz = lambda x: re.sub('\.', ',', re.sub(',', '', stight(x)))
ss = lambda x: string.strip(x)
sstrip = lambda x: string.strip(x)
stight = lambda x: re.sub('\s','',x)
LAddup = lambda a_l, b_l: [ x+y  for (x, y) in zip(a_l, b_l)]

## DATE TIME ########################################

# NOW
dtnow = lambda : datetime.datetime.now()
#2008-07-16 19:44:18.733000

dtnow_iso = lambda : time.strftime ("%Y-%m-%dT%H:%M:%S", time.localtime())
#2008-07-16T19:44:18

dtnow_plain = lambda : re.sub('[-. :]', '', str(datetime.datetime.now())[:19])
#20080716194418

yyyymmddThhmmss = lambda : re.sub('[-:]','', iso_time_now())
iso_time_now_gmt = lambda : time.strftime ("%Y-%m-%d %H:%M:%S", time.gmtime()) + ' +0000'
# CONVERSIONS
yyyymmdd2iso = lambda x: x[:4]+'-'+x[4:6]+'-'+x[6:]

#time.sleep(2)

## OS, FILE SYSTEM ###################################
#if pwg.main() == 'Microsoft Windows XP, Service Pack 2': print 'Win XP SP2'

#current working directory
cwd = re.sub(r'\\', '/', os.getcwd()) + '/'
#print os.path.split(r'C:\WINDOWS\system32\cmd.exe')
# >>> ('C:\\WINDOWS\\system32', 'cmd.exe')
#base, ext = os.path.splitext(r'C:\WINDOWS\system32\cmd.exe')
#print base, ext
# >>> C:\WINDOWS\system32\cmd .exe
# check for file/directory existence
if os.path.exists('abc'): print 'file/directory \'abc\' exists !'

fslist = lambda x_s, y_s: pfb.fslist(x_s, patterns=y_s, recurse=True, return_folders=False)

## SHELVE ############################################

shew = lambda key, data, db:    she.swrite(key, data, db)
shel = lambda db:               she.slist(db)
sher = lambda key, db:          she.sread(key, db)
shed = lambda key, db:          she.sdelete(key, db)

## OTHER ###########################################
# name of the running script
#print os.path.split(__file__)[-1]

if __name__ == "__main__":
    """ """
    
    print sys.platform
    # win32
    print sys.version
    # 2.4.4 (#71, Oct 18 2006, 08:34:43) [MSC v.1310 32 bit (Intel)]

    shel('pydl.db')
    print fslist('utils','*.bat;*.out;*213.html')
    print LCompare([2,1,3],[2,5])
    print LAddup([1,2,3],[2,5])
    print LDeduct([1,2,3],[2,5])
    print LMultiply([1,2,3],[2,5])
    print numczen('111 2.222.292.338,833')
    print numencz('222 4,444,292,338.833')

    list1 = [88, 99, 00, -1]
    list2 = [12, 5, -4, +5]
    s1= "QQQ"
    s2= "aaa"
    s3= "                   qwerty             "
    s4= "|3431101|  |    |            |S1 |           0,00 |     468.031,80 |    |0040046441|001NOVOTNA  |"
    
    print "list1=",  list1
    print "list2=",  list2, "\n"
    print "LCompare (list1, list2):", LCompare (list1, list2)
    print "LJoin(list1, list2):", LJoin(list1, list2)
    print "joinLists (list1, list2):", joinLists (list1, list2)
    print "LDeduct(list1, list2):", LDeduct(list1, list2)
    print "LAddup(list1, list2):", LAddup(list1, list2)
    print "LMultiply (list1, list2):", LMultiply (list1, list2), "\n--------------------------"
    print "string1=", s1
    print "string2=", s2, "\n"
    print "string.join(s1, s2):", string.join(s1, s2), "\n"
    print "string3=", s3, "\n"
    print "string.strip(s3):", string.strip(s3)
    print "string4=", s4, "\n"
    print "stight(s4):", stight(s4)
    print "(numczen+stight)=> numczen(s4):", numczen(s4)
